<template>
  <el-row
    type="flex"
    justify="space-between"
    align="middle"
    style="height:40px; width: 100%;"
  >
    <el-col>
      <!-- 左侧内容 名字-->
      <span>{{ treeNode.name }}</span>
    </el-col>
    <!-- 右侧 -->
    <el-col :span="4">
      <!-- 右侧 名字和操作 -->
      <el-row type="flex" justify="end">
        <el-col>{{ treeNode.manager }}</el-col>
        <el-col>
          <!-- 下拉菜单 -->
          <el-dropdown @command="operateDepts">
            <!-- 内容 -->
            <span>
              操作
              <i class="el-icon-arrow-down" />
            </span>
            <!-- 具名插槽 -->
            <el-dropdown-menu slot="dropdown">
              <!-- 下拉选项 -->
              <el-dropdown-item command="add">添加子部门</el-dropdown-item>
              <el-dropdown-item v-if="!isRoot" command="edit">编辑部门</el-dropdown-item>
              <el-dropdown-item v-if="!isRoot" command="del">删除部门</el-dropdown-item>
            </el-dropdown-menu>
          </el-dropdown>
        </el-col>
      </el-row>
    </el-col>
  </el-row>
</template>

<script>
import { delDepartments } from '@/api/departments'

export default {
  name: 'TreeTools',
  components: {},
  props: {
    treeNode: {
      type: Object,
      required: true
    },
    isRoot: {
      type: Boolean,
      default: false
    }
  },
  data () {
    return {

    }
  },
  computed: {},
  watch: {},
  created () {},
  mounted () {},
  methods: {
    async operateDepts (type) {
      if (type === 'add') {
        // 此时添加子部分
        this.$emit('addDepts', this.treeNode)
      } else if (type === 'edit') {
        // 此时编辑
        this.$emit('editDepts', this.treeNode)
      } else {
        // 删除操作
        await this.$confirm('确认删除该部门吗', '删除', {
          confirmButtonText: '确定',
          cancelButtonText: '取消'
        })
        await delDepartments(this.treeNode.id)
        // 更新视图
        this.$emit('delDepts')
        this.$message.success('删除部门成功')

        // .then(() => {
        //   return delDepartments(this.treeNode.id)// 返回promise对象
        // })
        // .then(() => {
        // // 删除成功
        // // 更新视图
        //   this.$emit('delDepts')
        //   this.$message.success('删除部门成功')
        // })
        // .catch(() => {
        //   this.$message.error('删除部门失败')
        // })
      }
    }
  }
}
</script>

<style scoped lang="scss">

</style>
